package com.Sdebruijn.curriculumvitae.util;

import android.content.Context;
import android.view.View;
import android.view.animation.AnimationUtils;

/**
 * Utilities for working with the {@link View} class
 */
public class ViewUtils {

	/**
	 * Type safe Enum for the different visibility states of {@link View}s <br>
	 * Possibilities: GONE, INVISIBLE, VISIBLE
	 * 
	 * @author Stefan de Bruijn
	 * 
	 */
	public enum State {
		GONE(android.view.View.GONE), INVISIBLE(android.view.View.INVISIBLE), VISIBLE(
				android.view.View.VISIBLE);

		private int state;

		private State(int i) {
			this.state = i;
		}
	}

	/**
	 * Utility method for switching visibility mostly between progress bars and
	 * everything else
	 * 
	 * @param viewA
	 *            View A, given state A
	 * @param stateA
	 *            State A, to be executed on View A
	 * @param viewB
	 *            View B, given state B
	 * @param stateB
	 *            State B, to be executed on View B
	 */
	// TODO some fading animation could be nice
	public static void switchVisibleState(final View viewA, ViewUtils.State stateA,
			final View viewB, ViewUtils.State stateB) {
		viewA.setVisibility(stateA.state);
		viewB.setVisibility(stateB.state);

	}

	/**
	 * Sets any given view's visibility status
	 * 
	 * @param view
	 *            View to change status from
	 * @param state
	 *            ViewUtils.State value: GONE, INVISIBLE or VISIBLE
	 */
	public static void setVisibleState(final View[] views, ViewUtils.State state,
			final View... exclude) {
		if (views.length > 0) {
			for (View v : views) {
				if (v != null && !ArrayUtils.containsView(exclude, v)) {
					v.setVisibility(state.state);
				}
			}
		}
	}

	/**
	 * Instigates a fade in animation on given view
	 * 
	 * @param view
	 *            View to fade in
	 * @param context
	 *            Activity context
	 */
	public static void fadeIn(final View[] view, final Context context) {
		if (view.length > 0) {
			for (View v : view) {
				if (v != null) {
					v.startAnimation(AnimationUtils.loadAnimation(context, android.R.anim.fade_in));
				}
			}
		}
	}

	/**
	 * Instigates a fade out animation on given view
	 * 
	 * @param view
	 *            View to fade out
	 * @param context
	 *            Activity context
	 */
	public static void fadeOut(final View[] view, final Context context) {
		if (view.length > 0) {
			for (View v : view) {
				if (v != null) {
					v.startAnimation(AnimationUtils.loadAnimation(context, android.R.anim.fade_out));
				}
			}
		}
	}

}